/**
 * Filename: StringUtils.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/2/29 18:35
 */
package com.github.postsanf.yinian.utils;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;

import com.github.postsanf.yinian.R;

import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {
    public static String numberToStr(int val) {
        double res;
        if (val < 10000) {
            return String.valueOf(val);
        } else if (val < 100 * 10000) {
            res = val / 10000;
            DecimalFormat dw = new DecimalFormat(".0");
            return dw.format(res) + "万";
        } else if (val < 10000 * 10000) {
            res = val / 10000;
            DecimalFormat dqw = new DecimalFormat(".0");
            return dqw.format(res) + "万";
        } else {
            res = val / 10000 * 10000;
            DecimalFormat dqw = new DecimalFormat(".0");
            return dqw.format(res) + "亿";
        }
    }

    public static String repaceImgUrl(String paths) {
        return paths.substring(0, paths.lastIndexOf("?")) + "&" + paths.substring(paths.lastIndexOf("?") + 1, paths.length());
    }

    // 图片质量默认处理
    public static String getQulity() {
        return "?imageView2/1/format/jpg/interlace/1/q/50";
    }

    public static String getQulity(int qual) {
        return "?imageView2/1/format/jpg/interlace/1/q/" + qual;
    }

    // 图片处理
    public static String getSmallPic(int val) {
        return val > 0 ? "?imageView2/1/h/" + val + "/w/" + val : "";
    }

    // 图片质量处理
    public static String getImgByWHQ(int width) {
        return width > 0 ? "?imageView2/1/h/" + width + "/w/" + width + "/format/jpg/interlace/1/q/50" : "?imageView2/1/format/jpg/interlace/1/q/60";
    }

    public static String getImgByWHQ(int width, int qval) {
        return width > 0 ? "?imageView2/1/h/" + width + "/w/" + width + "/format/jpg/interlace/1/q/50" : "?imageView2/1/format/jpg/interlace/1/q/" + qval;
    }

    /**
     * 去掉最后一个，
     */
    public static String delLastTag(String val) {
        return val.substring(0, val.length() - 1);
    }

    /**
     * String转Int
     */
    public static int string2Int(String val) {
        return Integer.parseInt(val);
    }

    /**
     * 判断字符串是否为空
     */

    public static boolean isEmpty(String input) {
        if (input == null || "".equals(input))
            return true;
        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
                return false;
            }
        }
        return true;
    }

    public static Boolean isEmptyString(String s) {
        if (s == null || s.trim().length() <= 0) {
            return true;
        }
        return false;
    }

    public static boolean isOfficialGroup(String type) {
        String official = "官方相册";
        if (official.equals(type)) {
            return true;
        }
        return false;
    }

    public static String getCateByType(String type) {
        String str = "邀请好友加入";
        if (type.length() > 2) {
            str = type.substring(0, 2);
            switch (str) {
                case "闺蜜":
                    str = "邀请闺蜜开启记录";
                    break;
                case "家人":
                    str = "邀请家人开启温馨圈子";
                    break;
                case "情侣":
                    str = "带上TA开启私密圈子";
                    break;
                case "其他":
                    str = "邀请朋友开启友情岁月";
                    break;
                case "死党":
                    str = "邀请死党一起high起来";
                    break;
            }
        }
        return str;
    }

//    public static String  getDefaultImg()
//    {
////        String imageUri = "drawable://" + R.drawable.avatar_default;
//        return imageUri;
//    }

    public static SpannableString getYiNianContent(final Context context, final TextView tv, String source) {
        String regexAt = "@[\u4e00-\u9fa5\\w]+";
        String regexEmoji = "\\[[\u4e00-\u9fa5\\w]+\\]";

        String regex = "(" + regexAt + ")|(" + regexEmoji + ")";

        SpannableString spannableString = new SpannableString(source);

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(spannableString);

        if (matcher.find()) {
            tv.setMovementMethod(LinkMovementMethod.getInstance());
            matcher.reset();
        }

        while (matcher.find()) {
            final String atStr = matcher.group(1);
            String emojiStr = matcher.group(2);

            if (atStr != null) {
                int start = matcher.start(1);
                NianClickableSpan clickableSpan = new NianClickableSpan(context) {
                    @Override
                    public void onClick(View widget) {
//						Intent intent = new Intent(context, UserInfoActivity.class);
//						intent.putExtra("userName", atStr.substring(1));
//						context.startActivity(intent);
                    }
                };
                spannableString.setSpan(clickableSpan, start, start + atStr.length(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
//			if(emojiStr != null) {
//				int start = matcher.start(2);
//				int imgRes = EmotionUtils.getImgByName(emojiStr);
//				Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), imgRes);
//
//				if(bitmap != null) {
//					int size = (int) tv.getTextSize();
//					bitmap = Bitmap.createScaledBitmap(bitmap, size, size, true);
//					ImageSpan imageSpan = new ImageSpan(context, bitmap);
//					spannableString.setSpan(imageSpan, start, start + emojiStr.length(),
//							Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//				}
//			}
        }
        return spannableString;
    }

    static class NianClickableSpan extends ClickableSpan {
        private Context context;

        public NianClickableSpan(Context context) {
            this.context = context;
        }

        @Override
        public void onClick(View widget) {
            // TODO Auto-generated method stub

        }

        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setColor(context.getResources().getColor(R.color.yncolor));
            ds.setUnderlineText(false);
        }
    }
}
